from django_redis import get_redis_connection
import json

from .BaseOpration import BaseOpration
from big_screen.utils import tools as t
from Control.Serialization import serMobile


class defaultOp(BaseOpration):
    def __init__(self):
        BaseOpration.__init__(self)
        self.con = get_redis_connection("default")


class sessionOp(BaseOpration):
    def __init__(self):
        BaseOpration.__init__(self)
        self.con = get_redis_connection("session")


class isworkingOp(BaseOpration):
    def __init__(self):
        BaseOpration.__init__(self)
        self.con = get_redis_connection("isworking")
        self.mob = serMobile()

    def formatter_info(self, info):
        con = dict()
        con["mobile"] = info.pop("phoneid")
        con["time"] = t.get_time(info.get("time"))
        con["lnglat"] = t.getLocation(info.pop("location"))
        address = t.getaddress(con["lnglat"])
        if address is 0:
            address = {
                'status': 1,
                'district': 94,
                'formatted_address': "",
                "data_from": "",
                "adcode": 520000
            }
        con["address"] = address 
        return (con.get("mobile"), con)

    def list_push(self, k, v):
        push_dict = v
        mobile = push_dict.get("mobile")
        if self.mob.is_mobile_exit(mobile):
            self.con.lpush(k, json.dumps(push_dict))


class timeOp(BaseOpration):
    def __init__(self):
        BaseOpration.__init__(self)
        self.con = get_redis_connection("time")


class broadcastOp(BaseOpration):
    def __init__(self):
        BaseOpration.__init__(self)
        self.con = get_redis_connection("broadcast")

    def formatter_scroll_info(self, info):
        """
        格式化轮播表数据
        :param info:
        :return:
        """
        content = list()
        content.append(info.get("time"))
        content.append(t.freq_sql_to_interface(info.get("freq")))
        content.append(info.get("category").name)
        content.append(info.get("address"))
        return content

    def formatter_heatmap_info(self, info):
        """
        格式化热力图数据
        :param info:
        :return:
        """
        content = dict()
        content["time"] = info.get("time")
        lnglat = info.get("lnglat").split(",")
        content["lng"] = lnglat[0]
        content["lat"] = lnglat[1]
        content["count"] = 1
        return content

    def formatter_scroll_info_from_ser(self, info):
        """
        格式化轮播表数据，数据来源为序列化器
        :param info:
        :return:
        """
        content = list()
        content.append(info.get("time"))
        content.append(info.get("freq"))
        content.append(info.get("category__name"))
        content.append(info.get("address"))
        return content


class brotimeoutOp(BaseOpration):
    def __init__(self):
        BaseOpration.__init__(self)
        self.con = get_redis_connection("brotimeout")


class chartOp(BaseOpration):
    def __init__(self):
        BaseOpration.__init__(self)
        self.con = get_redis_connection("chart")


class massmarkOp(BaseOpration):
    def __init__(self):
        BaseOpration.__init__(self)
        self.con = get_redis_connection("massmark")

    def formatter_data_from_ser(self, info):
        """
        用序列化器查询出的数据进行组织
        :param info:
        :return:
        """
        content = dict()
        lnglat = info.get("lnglat")
        content["time"] = info.get("time")
        content["address"] = info.get("address")
        content["category"] = info.get("category__name")
        content["freq"] = info.get("freq")
        return (lnglat, content)

    def formmater_data(self, info):
        """
        处理数据
        :param content:
        :return:
        """
        content = dict()
        lnglat = info.get("lnglat")
        content["time"] = info.get("time")
        content["address"] = info.get("address")
        content["category"] = info.get("category").name
        content["freq"] = t.freq_sql_to_interface(info.get("freq"))
        return (lnglat, content)

    def get_for_view(self):
        """
        为首页websocket组织数据
        :return:
        """
        content = list()
        keys = self.get_keys()
        if len(keys) is 0:
            return content
        else:
            for key in keys:
                info = dict()
                info["lnglat"] = key.split(",")
                data = self.list_get(key)
                data = list(map(lambda info: json.loads(info), data))
                info["address"] = list(map(lambda info: info.pop("address"), data))[0]
                info["id_count"] = len(data)
                info["data"] = data
                content.append(info)
        return content
